<template>
    <!-- <div class="loading-module">
        <div v-for="item, index in organizationList" :key="index" @click="chooseSystem(item)">{{ item.organizationName}}
        </div>
    </div> -->
</template>

<script>
import organizationApi from '@/api/organization.js'
import router from '@/router'
import store from '@/store'
export default {
    name: 'LoadingModule',
    components: {},
    data() {
        return {
            organizationList: [],
            topOrganizationNo: ''
        }
    },
    watch: {},
    created() { },
    mounted() {
        this.topOrganizationNo = store.getters.topOrganizationNo || sessionStorage.topOrganizationNo
        this.queryOrganization();
        this.queryByOrganizationNo();
    },
    destroyed() { },
    methods: {
        //顶级组织获取所有组织标识
        queryOrganization() {
            organizationApi.queryOrganization(this.topOrganizationNo).then(res => {
                let nolist = (res.data || []).map(e => e.organizationNo)
                this.queryTreeByOrganizationNoList(nolist)
            })
        },
        //获取组织库
        queryByOrganizationNo() {
            organizationApi.queryByOrganizationNo(this.topOrganizationNo).then(res => {
                console.log('组织库', res)
                let data = res.data || {};
                sessionStorage.setItem('databaseName', data.databaseName);
                sessionStorage.setItem('systemTenantMark', data.systemTenantMark);
                // sessionStorage.setItem('systemTenantMark','YCYY');
            })
        },
        //获取组织下子组织列表
        queryTreeByOrganizationNoList(nolist) {
            organizationApi.queryTreeByOrganizationNoList(nolist).then(res => {
                console.log(res.data)
                let list = res.data || []
                this.organizationList = [];
                list.forEach(e => {
                    this.organizationList.push(e)
                    if (e.child && e.child.length) {
                        this.organizationList = this.organizationList.concat(e.child)
                    }
                });
                let noList = this.organizationList.map(e => e.organizationNo)
                sessionStorage.setItem('organizationList', noList)
                let organizationNo = sessionStorage.organizationNo
                if (this.organizationList.length == 1) {
                    this.chooseSystem(this.organizationList[0])
                }else if(sessionStorage.entrance_flag){
                    let sys = this.organizationList.find(e=>e.organizationNo==organizationNo)
                    this.chooseSystem(sys)
                }
            })
        },
        chooseSystem(e) {
            sessionStorage.setItem('organizationNo', e.organizationNo)
            sessionStorage.setItem('organizationName', e.organizationName)
            this.setRouter();
        },
        async setRouter() {
            const accessRoutes = await store.dispatch('permission/generateRoutes', 'admin')
            router.addRoutes(accessRoutes)
            this.$router.push({ path: '/energy-overview' })
        },
    }
}
</script>

<style lang="scss" scoped></style>